import { login } from "../api/user"
import { genAsyncRoutes } from "../utils/permission"
import { LOGIN_FAILED, LOGIN_SUCCESS } from "./action-types"
import {saveWebStorage} from '../utils/index'
export const loginAsyncAction=({username,password,remember}) =>{
  return (dispatch)=>{
    login(username,password)
    .then(res=>{
      const routes= genAsyncRoutes(res.roles)
      console.log('用户角色：', res.roles, '，能够访问的路由：', routes)
      dispatch({
        type:LOGIN_SUCCESS,
        payload:{
          ...res,
          routes
        }
      })
      saveWebStorage(remember?localStorage:sessionStorage,'user',res)
    })
    .catch(err=>{
      dispatch({
        type:LOGIN_FAILED,
      })
    })
  }
  
}